#!/bin/bash

# Health Check 进程守护脚本
# 用于监控和自动重启 health-check 程序

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HEALTH_CHECK_BIN="$SCRIPT_DIR/../health-check"
CONFIG_FILE="$SCRIPT_DIR/../configs/config.yaml"
PID_FILE="$SCRIPT_DIR/../health-check.pid"
LOG_FILE="$SCRIPT_DIR/../logs/health-check-daemon.log"

# 创建日志目录
mkdir -p "$(dirname "$LOG_FILE")"

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# 检查程序是否运行
is_running() {
    if [ -f "$PID_FILE" ]; then
        local pid=$(cat "$PID_FILE")
        if ps -p "$pid" > /dev/null 2>&1; then
            return 0
        else
            rm -f "$PID_FILE"
            return 1
        fi
    fi
    return 1
}

# 启动程序
start() {
    if is_running; then
        log "Health Check 已经在运行中 (PID: $(cat "$PID_FILE"))"
        return 0
    fi

    log "启动 Health Check..."
    
    # 检查程序文件是否存在
    if [ ! -f "$HEALTH_CHECK_BIN" ]; then
        log "错误: 找不到程序文件 $HEALTH_CHECK_BIN"
        exit 1
    fi

    # 检查配置文件是否存在
    if [ ! -f "$CONFIG_FILE" ]; then
        log "错误: 找不到配置文件 $CONFIG_FILE"
        exit 1
    fi

    # 启动程序
    nohup "$HEALTH_CHECK_BIN" -daemon -config "$CONFIG_FILE" > "$LOG_FILE" 2>&1 &
    local pid=$!
    echo "$pid" > "$PID_FILE"
    
    # 等待一下检查是否启动成功
    sleep 2
    if is_running; then
        log "Health Check 启动成功 (PID: $pid)"
    else
        log "Health Check 启动失败"
        exit 1
    fi
}

# 停止程序
stop() {
    if ! is_running; then
        log "Health Check 未运行"
        return 0
    fi

    local pid=$(cat "$PID_FILE")
    log "停止 Health Check (PID: $pid)..."
    
    # 发送 TERM 信号
    kill -TERM "$pid" 2>/dev/null
    
    # 等待进程结束
    local count=0
    while [ $count -lt 10 ]; do
        if ! ps -p "$pid" > /dev/null 2>&1; then
            break
        fi
        sleep 1
        count=$((count + 1))
    done
    
    # 如果还在运行，强制杀死
    if ps -p "$pid" > /dev/null 2>&1; then
        log "强制停止 Health Check..."
        kill -KILL "$pid" 2>/dev/null
    fi
    
    rm -f "$PID_FILE"
    log "Health Check 已停止"
}

# 重启程序
restart() {
    log "重启 Health Check..."
    stop
    sleep 2
    start
}

# 状态检查
status() {
    if is_running; then
        local pid=$(cat "$PID_FILE")
        log "Health Check 正在运行 (PID: $pid)"
        return 0
    else
        log "Health Check 未运行"
        return 1
    fi
}

# 监控模式 - 持续监控并自动重启
monitor() {
    log "启动监控模式..."
    
    while true; do
        if ! is_running; then
            log "检测到 Health Check 已停止，正在重启..."
            start
        fi
        
        # 每30秒检查一次
        sleep 30
    done
}

# 主函数
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    monitor)
        monitor
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|monitor}"
        echo ""
        echo "命令说明:"
        echo "  start   - 启动 Health Check"
        echo "  stop    - 停止 Health Check"
        echo "  restart - 重启 Health Check"
        echo "  status  - 查看运行状态"
        echo "  monitor - 监控模式，自动重启"
        exit 1
        ;;
esac
